<?php
class RoleHelper
{
	/**
	 * 增加角色信息
	 */
	public static function add($title, $memo, $pv=null)
	{
		try {
			$data = array('name'=>$title,'memo'=>$memo);
			if(null!==$pv) $data['purview'] = join(',', $pv);
			return Role::add($data);
		}
		catch(Exception $_) {
			return false;
		}
	}
	
	/**
	 * 修改角色信息
	 */
	public static function modify($role_id, $title, $memo, $pv=null)
	{
		try {
			$data = array('name'=>$title,'memo'=>$memo);
			if(null!==$pv) $data['purview'] = join(',', $pv);
			return Role::update($role_id, $data);
		}
		catch(Exception $_) {
			return false;
		}
	}

	/**
	 * 删除一个角色、及角色授权
	 */
	public static function delete($role_id) {
		return Db::get('Role')->delete()->where(array('id'=>$role_id))->exec();
	}

	public static function getByUser($user_id){
		return Db::get('_user_role')->select(array('role_name'))->where(array('user_id' => $user_id))->hkey('role_id')->getCols();
	}

}
?>
